rm(list = setdiff(ls(), lsf.str()))
load("Study 1/Altered Data/Study1_Denmark.RData")

#SI A.4: Models belonging to Figure 1----------------
vote.dpp<-list()
vote.dpp[[1]]<-glm(vote_populist10_intention~zero1(agre)+zero1(open)+zero1(con)+zero1(ext)+zero1(neu)+female+age, data=data, family="binomial")
vote.dpp[[2]]<-glm(vote_populist10_intention~zero1(agre)+zero1(open)+zero1(con)+zero1(ext)+zero1(neu)+female+age+v2010_edu2 + v2010_edu3 + v2010_edu4 + v2010_edu5+income + income_missing + zero1(econ_cons) + zero1(social) + zero1(cynicism), data=data, family="binomial")

vote.dpp[[3]]<-glm(vote_populist11_choice~zero1(agre)+zero1(open)+zero1(con)+zero1(ext)+zero1(neu)+female+age, data=data, family="binomial")
vote.dpp[[4]]<-glm(vote_populist11_choice~zero1(agre)+zero1(open)+zero1(con)+zero1(ext)+zero1(neu)+female+age+v2010_edu2 + v2010_edu3 + v2010_edu4 + v2010_edu5+income + income_missing + zero1(econ_cons) + zero1(social)  + zero1(cynicism), data=data, family="binomial")

labels <- c("Agreeableness", "Openness", "Conscientiousness", "Extraversion", "Neuroticism", "Female", "Age",  "Education (Ref: primary): Vocational", "Upper secondary", "Professional", "Bachelor or higher", "Income", "Income missing", "Economic conservatism","Social conservatism",  "Political cynicism")
stargazer2(vote.dpp, odd.ratio=TRUE, title="Danish Election Study: Support for Danish People's Party", align=TRUE, omit.stat=c("LL","ser","f", "adj.rsq"),  star.cutoffs=c(0.05), covariate.labels = labels, dep.var.labels.include = FALSE, model.numbers= FALSE, font.size = "tiny",column.labels = c("Base - 2010", "Figure 1 - 2010", "Base - 2011", "Figure 1 - 2011"), notes = "Odds ratios and standard errors from logistic regression models; *p<0.05",  notes.append = FALSE, out="Tables/Danish_results.tex", no.space=TRUE, label="tab:DK_results" )

# 2010 odds
#calculate 1 SD above and below the mean
low  <- mean(zero1(data$agre), na.rm=T)-sd(zero1(data$agre), na.rm=T)
high <- mean(zero1(data$agre), na.rm=T)+sd(zero1(data$agre), na.rm=T)

odds.difference <- (exp(vote.dpp[[2]]$coefficients)[2]*low) / (exp(vote.dpp[[2]]$coefficients)[2]*high)

# 2011 odds
#calculate 1 SD above and below the mean
low  <- mean(zero1(data$agre)[is.na(data$vote_populist11_choice)==FALSE], na.rm=T)-sd(zero1(data$agre)[is.na(data$vote_populist11_choice)==FALSE], na.rm=T)
high <- mean(zero1(data$agre)[is.na(data$vote_populist11_choice)==FALSE], na.rm=T)+sd(zero1(data$agre)[is.na(data$vote_populist11_choice)==FALSE], na.rm=T)

odds.difference <- (exp(vote.dpp[[4]]$coefficients)[2]*low) / (exp(vote.dpp[[4]]$coefficients)[2]*high)

#SI A.4: Correlation matrix ---------------------------

#Physiology data.frame
cor_ivs<-(with(data, data.frame(agre, open,con, ext ,neu, cynicism , social , econ_cons)))

names(cor_ivs) <- c("1. Agreeableness","2. Opennesss","3. Conscientiousness",
                    "4. Extraversion", "5. Neuroticism", "6. Cynicism",
                    "7. Social conservatism", "8. Economic conservatism")
correlation.matrix <- cor(cor_ivs, use="complete.obs")
correlation.matrix <- data.frame(get_lower_tri(correlation.matrix))
correlation.matrix<-correlation.matrix[,c(1:7)]
names(correlation.matrix) <- seq(1,7,1)

correlation.matrix<-as.matrix(correlation.matrix)
stargazer(correlation.matrix, title="Danish Election Study: Correlation Matrix of Independent Variables", out="Tables/DK_cor.tex", no.space=TRUE, label="tab:DK_cor", digits=2)

#SI A.4: Descriptive statistics-----
desc.labels <- c("Populist Vote", labels)
stargazer(vote.dpp[[2]]$model, type = "latex", covariate.labels=desc.labels, summary.stat = c("mean", "sd", "N", "median","min",  "max"), title="Danish Election Study: Descriptive statistics", out="Tables/DK_descrip.tex", no.space=TRUE, label="tab:DK_descriptives", digits=2)

#Alpha Agreeableness
alpha_a <- data.frame(data$agr1_rec, data$agr2_rec, data$agr3_rec, data$agr4_rec, data$agr5_rec, data$agr6_rec, data$agr7_rec, data$agr8_rec, data$agr9, data$agr10, data$agr11, data$agr12)
psych::alpha(alpha_a)
#Alpha Openenss
alpha_o<-data.frame(data$open1_rec, data$open2_rec, data$open3_rec, data$open4_rec,  data$open5_rec,  data$open6_rec, data$open7,  data$open8, data$open9,  data$open10, data$open11,  data$open12)
psych::alpha(alpha_o)
#Alpha Conscientiousness
alpha_c<-data.frame(data$con1_rec, data$con2_rec, data$con3_rec, data$con4_rec, data$con5_rec, data$con6, data$con7, data$con8, data$con9, data$con10, data$con11, data$con12)
psych::alpha(alpha_c)
#Extraversion
alpha_e<-data.frame(data$ext1_rec, data$ext2_rec, data$ext3_rec, data$ext4, data$ext5, data$ext6, data$ext7, data$ext8, data$ext9, data$ext10, data$ext11, data$ext12)
psych::alpha(alpha_e)
#Neuroticism
alpha_n<-data.frame(data$neu1_rec, data$neu2_rec, data$neu3_rec, data$neu4_rec, data$neu5, data$neu6, data$neu7, data$neu8, data$neu9, data$neu10, data$neu11, data$neu12)
psych::alpha(alpha_n)
#Economic conservatism
econ_a<-data.frame(data$econ1, data$econ2)
psych::alpha(econ_a)
#Social conservatism
alpha_social<-data.frame(data$v2010_q62_3_rec, data$v2010_q62_4_rec, data$v2010_q62_5_4point, data$v2010_q62_6_rec, data$v2010_q62_7_4point, data$v2010_q62_8_4point, data$v2010_q62_10_4point)
psych::alpha(alpha_social, keys = TRUE)
#Cynicism
cyn_a <- data.frame(data$cyn1, data$cyn2, data$cyn3)
psych::alpha(cyn_a)

#SI A.4: Factor loadings -----------------
latent<-(with(data, data.frame(agr1_rec, agr2_rec,agr3_rec,agr4_rec,agr5_rec,agr6_rec,agr7_rec,agr8_rec,agr9,agr10,agr11,agr12, open1_rec , open2_rec , open3_rec , open4_rec , open5_rec,open6_rec,open7,open8,open9,open10,open11, open12, con1_rec,con2_rec,con3_rec,con4_rec,con5_rec,con6,con7,con8,con9,con10,con11,con12, ext1_rec,ext2_rec,ext3_rec,ext4,ext5,ext6,ext7,ext8,ext9,ext10,ext11,ext12, neu1_rec,neu2_rec,neu3_rec,neu4_rec,neu5,neu6,neu7,neu8,neu9,neu10,neu11,neu12)))
latent <- na.omit(latent)

CFA_agree <-'Agree = ~ NA*agr1_rec+agr2_rec+agr3_rec+agr4_rec+agr5_rec+agr6_rec+agr7_rec+agr8_rec+agr9+agr10+agr11+agr12 
Open = ~ NA*open1_rec + open2_rec + open3_rec + open4_rec + open5_rec+open6_rec+open7+open8+open9+open10+open11+open12
Con = ~ NA*con1_rec+con2_rec+con3_rec+con4_rec+con5_rec+con6+con7+con8+con9+con10+con11+con12
Ext = ~ NA*ext1_rec+ext2_rec+ext3_rec+ext4+ext5+ext6+ext7+ext8+ext9+ext10+ext11+ext12
Neu = ~ NA*neu1_rec+neu2_rec+neu3_rec+neu4_rec+neu5+neu6+neu7+neu8+neu9+neu10+neu11+neu12
Agree ~~ 1*Agree
Open ~~ 1*Open
Con ~~ 1*Con
Ext ~~ 1*Ext
Neu ~~ 1*Neu
' 

fit<-cfa(CFA_agree, ordered=c(latent$agr1_rec, latent$agr2_rec,latent$agr3_rec,latent$agr4_rec,latent$agr5_rec,latent$agr6_rec,latent$agr7_rec,latent$agr8_rec,latent$agr9,latent$agr10,latent$agr11,latent$agr12, latent$open1_rec , latent$open2_rec , latent$open3_rec , latent$open4_rec , latent$open5_rec,latent$open6_rec,latent$open7,latent$open8,latent$open9,latent$open10,latent$open11, latent$open12, latent$con1_rec,latent$con2_rec,latent$con3_rec,latent$con4_rec,latent$con5_rec,latent$con6,latent$con7,latent$con8,latent$con9,latent$con10,latent$con11,latent$con12, latent$ext1_rec,latent$ext2_rec,latent$ext3_rec,latent$ext4,latent$ext5,latent$ext6,latent$ext7,latent$ext8,latent$ext9,latent$ext10,latent$ext11,latent$ext12, latent$neu1_rec,latent$neu2_rec,latent$neu3_rec,latent$neu4_rec,latent$neu5,latent$neu6,latent$neu7,latent$neu8,latent$neu9,latent$neu10,latent$neu11,latent$neu12), data=latent)

#assess fit
p<-parameterEstimates(fit, standardized=TRUE) %>%  dplyr::select(std.all, pvalue)
p <- p[1:60, ] 
names(p) <- c("Standardized Factor Loading", "p-value")
(setattr(p, "row.names", c("Agreeableness item 1", "Agreeableness item 2", "Agreeableness item 3", "Agreeableness item 4", "Agreeableness item 5", "Agreeableness item 6", "Agreeableness item 7", "Agreeableness item 8", "Agreeableness item 9", "Agreeableness item 10", "Agreeableness item 11", "Agreeableness item 12", "Openness item 1", "Openness item 2", "Openness item 3", "Openness item 4", "Openness item 5", "Openness item 6", "Openness item 7", "Openness item 8", "Openness item 9", "Openness item 10", "Openness item 11", "Openness item 12", "Conscientiousness item 1", "Conscientiousness item 2", "Conscientiousness item 3", "Conscientiousness item 4", "Conscientiousness item 5", "Conscientiousness item 6", "Conscientiousness item 7", "Conscientiousness item 8", "Conscientiousness item 9", "Conscientiousness item 10", "Conscientiousness item 11", "Conscientiousness item 12", "Extraversion item 1", "Extraversion item 2", "Extraversion item 3", "Extraversion item 4", "Extraversion item 5", "Extraversion item 6", "Extraversion item 7", "Extraversion item 8", "Extraversion item 9", "Extraversion item 10", "Extraversion item 11", "Extraversion item 12", "Neuroticism item 1", "Neuroticism item 2", "Neuroticism item 3", "Neuroticism item 4", "Neuroticism item 5", "Neuroticism item 6", "Neuroticism item 7", "Neuroticism item 8", "Neuroticism item 9", "Neuroticism item 10", "Neuroticism item 11", "Neuroticism item 12")))

p<-xtable(caption = "Big Five: Standardized Factor Loadings", label = "tab:Denmark_cfa", p)
print(p, type="latex", file="Tables/DK_factor.tex", size="tiny", caption.placement="top")



